// pages/meeting/center/center.js
var api = require('../../../utils/httpUtil.js');
var dateUtils = require('../../../utils/dateUtils.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabCur: 0,
    scrollLeft: 0,
    tabList: ['未开始', '进行中', '已结束'],
    openId: app.globalData.openId,
    meetingBefore: [],
    showCancel: false,
    curMeeting: 0,
    meetingMid: [],
    meetingAfter: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    api.httpRequest(api.meetingListAPI, {
        openId: this.data.openId
      })
      .then(res => {
        if (res.code == 200) {
          var curDateTime = dateUtils.currentDateTime();
          let newList = res.data.map(item => {
            if (item.applyStartTime > curDateTime) {
              item.status = '未开始';
            } else if (item.applyEndTime < curDateTime) {
              item.status = '已结束';
            } else {
              item.status = '进行中';
            }
            if(item.applyStatus==1)
            {
              item.status="已取消";
            }
            item.date = item.applyStartTime.substring(0, 10);
            item.startTime = item.applyStartTime.substring(11, 16);
            item.endTime = item.applyEndTime.substring(11, 16);
            return item;
          });

          this.setData({
            meetingBefore: newList.filter(item => item.status === '未开始'),
            meetingMid: newList.filter(item => item.status === '进行中'),
            meetingAfter: newList.filter(item => item.status === '已结束' || item.status === '已取消'),
          })
        } else {
          wx.showToast({
            title: res.msg,
            duration: 2000,
            mask: true,
            icon: 'none'
          })
        }

      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  tabSelect(e) {
    this.setData({
      tabCur: e.currentTarget.dataset.id,
    })
  },
  confirmClick() {
    var that=this;
    api.httpRequest(api.meetingCancelAPI, {
        id: this.data.curMeeting
      })
      .then(res => {
        wx.showToast({
          title: res.msg,
          duration: 2000,
          mask: true,
          icon: 'none'
        })
        if (res.code == 200) {
          // 假设要修改第一个元素的 value
          let newList = that.data.meetingBefore.map(item => {
            if (item.id === that.data.curMeeting) {
              item.status = '已取消'; // 修改 value
            }
            return item;
          });

          that.setData({
            meetingBefore: newList,
            showCancel: false
          });
        }
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });

  },
  showModal(e) {
    this.setData({
      showCancel: true,
      curMeeting: e.currentTarget.dataset.id
    })
  },
  hideModal(e) {
    this.setData({
      showCancel: false
    })
  },
})